package com.wyy.test

import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.reactive.RedisReactiveCommands
import org.apache.commons.lang3.StringUtils
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

import java.util.stream.Collectors

/**
 * @Date: 2022/6/28
 * @Author: wyy  
 */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Disabled
class LettuceSentinelTest {

    private RedisClient redisClient

    private StatefulRedisConnection<String, String> connection

    @BeforeAll
    void initClient() {
        println "LettuceTest.initClient"

        String master = "master"
        String nodes = "localhost:6379,localhost:6380"
        String password = "123123"

        RedisURI.Builder builder = RedisURI.builder();
        nodes.split(",")
                .each { node ->
                    String[] node_port = StringUtils.split(node, ":");
                    builder.withSentinel(node_port[0], Integer.valueOf(node_port[1]));
                }

        builder.withSentinelMasterId(master);
        builder.withPassword(password.toCharArray())

        RedisURI redisURI = builder.build();
        this.redisClient = RedisClient.create(redisURI)
        this.connection = redisClient.connect()
    }

    @AfterAll
    void close() {
        println "LettuceSingleTest.close"

        this.connection.close()
        this.redisClient.shutdown()
    }

    @Test
    void testList() {
        RedisReactiveCommands<String, String> reactiveCommands = connection.reactive()
        List<String> list = reactiveCommands.lrange("test:list", 0, -1)
                .toStream()
                .collect(Collectors.toList())
        println "list = $list"
    }

}
